#!/usr/bin/env python

#Copyright (C) 2008 Luis Felipe Millani (lf.millani@gmail.com)

#This file is part of MagicLyrics.

#MagicLyrics is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#MagicLyrics is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with MagicLyrics.  If not, see <http://www.gnu.org/licenses/>.
'''
	to-do:
		use exceptions on netsearch
			to do so we'll need to use exceptions on get
		fix args order
		maybe search on google mobile instead?
'''
from urllib import quote_plus,unquote
def netsearch(query,fetch,max=5,engine='google'):
	"""
	netsearch(...) -> list of strings (urls); None on failure
	
	query a search engine for given terms
	
	query     search terms
	max       total of urls to fetch (currently max must be <=10)
	engine    search engine to use
	fetch     function which, given a url (string), downloads that url and returns the downloaded data
	"""
	def get_google_results():
		# as we only check the first page, let's limit the max of urls to 10
		# fetch the webpage
		src=fetch('http://www.google.com/search?hl=en&q='+query)
		# copy the urls into results
		urls=[]
		for i in range(0,max):
			if src.find(' class=g>')==-1: # no more results
				break
			src=src[src.find(' class=g>')+len(' class=g>'):]
			src=src[src.find('href="')+len('href="'):]
			if src[:src.find('"')].find('/url?')==0:	# skip youtube results
				i-=1
			else:
				urls.append(src[:src.find('"')])
		return urls
	def get_yahoo_results():
		src=fetch('http://search.yahoo.com/search?n=10&ei=UTF-8&p='+query)
		urls=[]
		while src.find('<a class="yschttl" href="')>-1:
			src=src[src.find('<a class="yschttl" href="')+len('<a class="yschttl" href="'):]
			urls.append(unquote(src[src.find('**')+2:src.find('"')]))
		return urls
	
	if max>10:max=10
	
	# encode search query
	query=quote_plus(query.encode('utf8'))
	
	return {
	'google':get_google_results,
	'yahoo':get_yahoo_results
	}[engine]()